package com.jy.modules.cims.component.image.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.jy.modules.cims.common.exception.CimsExceptionConstant;
import com.jy.modules.cims.common.sysUtils.SpringContextUtils;
import com.jy.modules.cims.component.image.TransferImagesDto;
import com.jy.modules.cims.component.image.TransferImagesHelper;
import com.jy.modules.cims.component.image.TransferImagesQueue;
import com.jy.modules.cims.sysUtils.ImgurFtpConfigInfo;

/**
 * @description 压缩服务
 * @author shengzhoutao
 * @date 2015-09-29
 */
@Component
public class ZipImagesByLocalService {
	private static Logger logger = LoggerFactory.getLogger(ZipImagesByLocalService.class);

	@Autowired
	private SpringContextUtils springContextUtils;
	
	/**
	 * @description
	 * @param transferImagesDto
	 */
	public void doSomething(TransferImagesDto transferImagesDto) {
	
		String cooperorgCode = transferImagesDto.getCooperorgCode();
		ImgurFtpConfigInfo imgurFtpConfigInfo = null;
		boolean zipFlag = false;
		TransferImagesHelper transferImagesHelper = springContextUtils.getBean(TransferImagesHelper.class);
		// //
		try {
			// 根据合作机构编码获取配置文件信息
			imgurFtpConfigInfo =  transferImagesHelper.getImgurFtpConfigInfoByCooperorgCode(cooperorgCode);
			
			String zipFileName = transferImagesHelper.getZipFileURL(imgurFtpConfigInfo,transferImagesDto);
			String zipDires = transferImagesHelper.getZipDir(imgurFtpConfigInfo, transferImagesDto);// 下载到当地那个路径下
			zipFlag = transferImagesHelper.zipFile(zipDires, zipFileName);
			// 压缩成功
			if (zipFlag) {
				logger.info("压缩成功的合同号:" + transferImagesDto.getContractNo());
				// 放入上传队列
				TransferImagesQueue.uploadQueue.offer(transferImagesDto);
			}
			//
		} catch (Exception e) {
			logger.error(CimsExceptionConstant.TRANSFER_IMG_FAIL, e);
		} finally {
			// 压缩失败，清理队列数据和本地文件
			if (!zipFlag) {
				transferImagesHelper.delDealData(imgurFtpConfigInfo, transferImagesDto);
			}
		}
		// //
	}
}
